package programer.interviewer;

public class Question6 {
    public String compressString(String first) {
        if (first.length() <= 2) {
            return first;
        }

        StringBuilder stringBuilder = new StringBuilder();
        char preChar = 0;
        char curChar = 0;
        int count = 0;
        for(int i = 0; i < first.length() ; i++) {
            curChar = first.charAt(i);
            if (preChar != 0 && (preChar != curChar)) {
                stringBuilder.append(preChar).append(count);
                count = 1;
            } else  {
                count ++;

            }
            preChar = curChar;
        }

        stringBuilder.append(preChar).append(count);


        if (stringBuilder.length() > first.length()) {
            return first;
        } else {
            return  stringBuilder.toString();
        }
    }


    public static void main(String[] args) {
        System.out.println(new Question6().compressString("MMmmsmmammmMM"));
    }
}
